package org.efficientsoap.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.util.Log;

public class EfficientSoapHttpProxy {

	private StringWriter outputWriter;
	
	//without getter/setter
	private BufferedReader rd;
	private BufferedWriter wr;
	
	private String hostname = "192.168.1.16";
	private	int port = 80;
	private String methodpath = "/test/testserver";
	private String Encoding = "UTF-8";
	private int SocketTimeoutMs = 500;
	
	private static final String TAG = EfficientSoapHttpProxy.class
			.getSimpleName();

	public EfficientSoapHttpProxy(StringWriter out) {
		super();
		this.outputWriter = out;
	}

	public EfficientSoapHttpProxy() {
		outputWriter = new StringWriter();
	}
	
	
	public StringWriter getOutputWriter() {
		return outputWriter;
	}
	public void setOutputWriter(StringWriter out) {
		this.outputWriter = out;
	}
	
	public String getHostname() {
		return hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}
	
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	
	public String getMethodpath() {
		return methodpath;
	}
	public void setMethodpath(String methodpath) {
		this.methodpath = methodpath;
	}
	
	public int getSocketTimeoutMs() {
		return SocketTimeoutMs;
	}
	public void setSocketTimeoutMs(int socketTimeoutMs) {
		SocketTimeoutMs = socketTimeoutMs;
	}

	public String getEncoding() {
		return Encoding;
	}
	public void setEncoding(String encoding) {
		Encoding = encoding;
	}

	public BufferedReader send() throws IOException, SocketTimeoutException{
	
		//String NAMESPACE = "http://"+HOSTNAME+":"+port+methodpath+"/";
		//String METHOD_NAME = "generateComplexData";
		//String URL = "/" + HOSTNAME + "/testserver";
		//String SOAP_ACTION = "\"" + URL + "/" + METHOD_NAME + "/" + "\"";

		// Create socket	
		InetAddress addr = null;
		Socket socket = null;
		addr = InetAddress.getByName(hostname);
		//socket = new Socket(addr, port);
		SocketAddress sockaddr = new InetSocketAddress(addr, port);
		socket = new Socket();	 
	    socket.connect(sockaddr, SocketTimeoutMs);

		// Send header
		wr = null;
		try {
			// You can use "UTF8" for compatibility with the Microsoft virtual
			// machine.
			wr = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream(), Encoding));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block e.printStackTrace(); }
		}

		try {
			wr.write("POST " + methodpath + " HTTP/1.0\r\n");
			wr.write("Host: " + hostname + "\r\n");
			wr.write("Content-Length: " + outputWriter.getBuffer().length() + "\r\n");
			wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
			wr.write("\r\n");

			// Send data
			wr.write(outputWriter.getBuffer().toString());
			Log.d(TAG, outputWriter.toString());
			wr.flush();

			// Response
			// BufferedReader rd;
			rd = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			String line;
			
			/*			
			//for developing only
			while ((line = rd.readLine()) != null) {
				line = rd.readLine();
			}
			*/
			
			//remove HTTP header from response stream
			
			//TODO analysing header e.g.:Error: Could not connect to server.
			Log.d(TAG, "response HTTP header:");
			for(int i=0;i<=5;i++) {
				line = rd.readLine();
				Log.d(TAG, line);
			}

		} catch (IOException e) { // TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rd;
	}
}
